{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2c026eae",
   "metadata": {},
   "source": [
    "## 方程\n",
    "\n",
    "$\n",
    "y = w * x + b + \\epsilon\n",
    "$\n",
    "\n",
    "$\n",
    "loss = (WX + b - y)^2\n",
    "$\n",
    "\n",
    "WX + b： 预测值\n",
    "\n",
    "y：真实值\n",
    "\n",
    "$\n",
    "\\epsilon\n",
    "$\n",
    "：高斯噪声"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0b23122",
   "metadata": {},
   "source": [
    "## 回归问题实战"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5590813a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "starting gradient descent at b = 0, w = 0, error = 5565.107834483211\n",
      "Running...\n",
      "After 1000 iterations b = 0.08893651993741346, w = 1.4777440851894448, error = 112.61481011613473\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# y = wx + b\n",
    "\n",
    "# 计算loss\n",
    "def compute_error_for_line_given_points(b, w, points):\n",
    "    totalError = 0\n",
    "    for i in range(0, len(points)):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        totalError += (y - (w * x + b)) ** 2\n",
    "    return totalError / float(len(points))\n",
    "\n",
    "# 计算梯度\n",
    "def step_gradient(b_current, w_current, points, learningRate):\n",
    "    b_gradient = 0\n",
    "    w_gradient = 0\n",
    "    N = float(len(points))\n",
    "    for i in range(0, len(points)):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        b_gradient += -(2/N) * (y - ((w_current * x) + b_current))\n",
    "        w_gradient += -(2/N) * x * (y - ((w_current * x) + b_current))\n",
    "    new_b = b_current - (learningRate * b_gradient)\n",
    "    new_w = w_current - (learningRate * w_gradient)\n",
    "    return [new_b, new_w]\n",
    "\n",
    "# 循环计算梯度\n",
    "def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):\n",
    "    b = starting_b\n",
    "    w = starting_w\n",
    "    for i in range(num_iterations):\n",
    "        b, w = step_gradient(b, w, np.array(points), learning_rate)\n",
    "    return [b, w]\n",
    "\n",
    "def run():\n",
    "    points = np.genfromtxt('3_simple_regression_data.csv', delimiter=',')\n",
    "    learning_rate = 0.0001\n",
    "    # initial y-intercept guess\n",
    "    initial_b = 0\n",
    "    # initial slope guess\n",
    "    initial_w = 0\n",
    "    num_iterations = 1000\n",
    "    print('starting gradient descent at b = {0}, w = {1}, error = {2}'\n",
    "          .format(initial_b, initial_w, compute_error_for_line_given_points(initial_b, initial_w, points)))\n",
    "    print('Running...')\n",
    "    [b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)\n",
    "    print('After {0} iterations b = {1}, w = {2}, error = {3}'\n",
    "         .format(num_iterations, b, w, compute_error_for_line_given_points(b, w, points)))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    run()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2648658",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
